<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/28
 * Time: 9:27
 */

namespace app\team\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\team\model\Good;

class Goods extends Common
{
    public function Goods()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['is_del'] = array('eq', 0);
            $where['team_id'] = Session::get('team')['team_id'];
            if (isset($pagedata['gtour_title']) && $pagedata['gtour_title'] != '') {
                $where['gtour_title'] = array('like', '%' . $pagedata['gtour_title'] . '%');
            }

            $count = Good::getGoodsCount($where);
            $data = Good::getGoodsList($where, $startrow, $pagesize);
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else {
            $this->assign('team_id',Session::get('team')['team_id']);
            return view();
        }
    }

    /*
     * 添加商品
     */
    public function addGoods()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            if (!$post['gtour_covers']) {
                return ['code' => 1, 'msg' => '请上传跟团游封面'];
                exit;
            }
            $post['start'] = $post['province'] . $post['city'] . $post['district'] . $post['address'];
            $post['team_id'] = Session::get('team')['team_id'];
            $post['create_time'] = time();
            unset($post['province']);
            unset($post['city']);
            unset($post['district']);
            unset($post['address']);
            unset($post['file']);
            $res = Db::name('group_tour')->insertGetId($post);
            if ($res) {
                return json(array('code' => 0, 'msg' => '添加成功'));
            } else {
                return json(array('code' => 2, 'msg' => '添加失败'));
            }
        } else {
            return view();
        }
    }

    /*
     * 添加行程
     */
    public function addTravel()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            $post['create_time'] = time();
            $post['gt_id'] = Session::get('gt_id');
            //查询看看是否添加的是同一天
            $day = Db::name('gtour_journey')->where('gt_id', Session::get('gt_id'))->where('sort_day', $post['sort_day'])->find();
            if ($day) {
                return json(array('code' => 1, 'msg' => '已经添加过这一天的行程'));
            }
            $res = Good::travel($post);
            if (!empty($res)) {
                return json(array('code' => 0, 'msg' => '添加成功'));
            } else {
                return json(array('code' => 2, 'msg' => '添加失败'));
            }
        } else {
            $id = input('gt_id');
            Session::set('gt_id', $id);
            return view();
        }
    }

    /*
     * 查看行程
     */
    public function lookTravel()
    {
        $id = input('gt_id');
        $res = Db::name('gtour_journey')->where('gt_id', $id)->order('sort_day', 'asc')->select();
        $this->assign('res', $res);
        return view();
    }

    /*
     * 修改行程
     */
    public function editTravel()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = [
                'sort_day' => $post['sort_day'],
                'tour' => $post['tour'],
                'gt_id' => Session::get('gid'),
                'catering' => $post['catering'],
                'living' => $post['living'],
                'update_time' => time(),
            ];
            $res = Db::name('gtour_journey')->where('journey_id', $post['id'])->update($data);
            if ($res) {
                return json(array('code' => 0, 'msg' => '修改成功'));
            } else {
                return json(array('code' => 2, 'msg' => '修改失败'));
            }
        } else {
            $id = input('gt_id');
            Session::set('gid', $id);
            $res = Db::name('gtour_journey')->where('gt_id', $id)->order('sort_day', 'asc')->select();
            $this->assign('res', $res);
            return view();
        }

    }
    /*
 * 删除
 */
    public function delTravel()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();

            $res = Db::name('gtour_journey')->where('journey_id', $post['id'])->delete();
            if ($res) {
                return json(array('code' => 0, 'msg' => '删除成功'));
            } else {
                return json(array('code' => 2, 'msg' => '删除失败'));
            }
        } else {
            $id = input('gt_id');
            Session::set('gid', $id);
            $res = Db::name('gtour_journey')->where('gt_id', $id)->order('sort_day', 'asc')->select();
            $this->assign('res', $res);
            return view();
        }

    }

    /*
     * 修改其它信息
     */
    public function editGoods()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            $post['team_id'] = Session::get('team')['team_id'];
            $post['create_time'] = time();
            $post['start'] = $post['address'];
            unset($post['address']);
            unset($post['file']);
            $res = Db::name('group_tour')->where('id', Session::get('gid'))->update($post);
            if ($res) {
                return json(array('code' => 0, 'msg' => '修改成功'));
            } else {
                return json(array('code' => 2, 'msg' => '修改失败'));
            }

        } else {
            $id = input('id');
            Session::set('gid', $id);
            $res = Db::name('group_tour')->where('id', $id)->find();
            $this->assign('res', $res);
            return view();
        }
    }

    /*
     * 删除商品
     */
    public function delGoods()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('group_tour')->where('id', $post['id'])->setField('is_del', 1);
            if ($res !== false) {
                return "1";
            } else {
                return "2";
            }
        }
    }

    /*
     * 是否上架
     */
    public function editIsDisplay()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            //商品是否有往期相册
            $img = Db::name('team_img')->where('team_id',$post['id'])->count();
            if($img<3){
                return ['code' => 2];
            }else{
                $res = Db::name('group_tour')->where('id', $post['id'])->setField('is_display', $post['is_display']);
                if ($res) {
                    return ['code' => 0];
                } else {
                    return ['code' => 1];
                }
            }

        }
    }

    /*
     * 往期相册
     */
    public function lookImage()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $id = Session::get('img_id');
            $count = Db::name('gtour_tour_img')->where('gt_id',$id)->where('is_del',0)->count();
            $data = Good::getGtImage($id, $startrow, $pagesize);
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else {
            $id = input('gt_id');
            Session::set('img_id', $id);
            return view();
        }
    }

    /*
     * 添加往期相册
     */
    public function addImage()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            $post['gt_id'] = Session::get('img_id');

            $post['create_time'] = date('Y-m-d H:i:s');
            if (!$post['img_path']) {
                return ['code' => 2, 'msg' => '请上传往期照片'];
                exit;
            }
            unset($post['file']);
            $res = Db::name('gtour_tour_img')->insertGetId($post);
            if ($res) {
                return json(array('code' => 0, 'msg' => '添加成功'));
            } else {
                return json(array('code' => 1, 'msg' => '添加失败'));
            }
        }
    }

    /*
     * 双击修改权重
     */
    public function editSort()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('gtour_tour_img')->where('id', $post['id'])->setField('sort', $post['sort']);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => '修改失败'];
            }
        }
    }

    /*
     * 删除往期照片
     */
    public function delImage()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('gtour_tour_img')->where('id', $post['id'])->setField('is_del', 1);
            if ($res !== false) {
                return "1";
            } else {
                return "2";
            }
        }
    }

    /*
     * 班期
     */
    public function banQi()
    {
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            //查询所有的数据
            $res = Db::name('gtour_gschedule')->field('tg_date,tg_price')->where('tg_id',$post['gt_id'])->select();
            if(empty($res)){
                $date = [];
            }
            foreach ($res as $k=>$value){
                $datetime = substr($value['tg_date'],-2);
                if ($datetime*1>=10){
                    $date[$value['tg_date']] = '￥'.$value['tg_price'].'<br>'.$datetime;
                }else{
                    $date[$value['tg_date']] = '￥'.$value['tg_price'].'<br>'.substr($datetime,-1);
                }

            }
            return ['code'=>0,'data'=>$date];
        }
    }

    /*
     * 添加班期
     */
    public function setPaiban()
    {
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $date = [
                'tg_id'=>$post['tg_id'],
                'tg_date'=>$post['tg_date'],
                'tg_price'=>$post['tg_price'],
                'create_time'=>time(),
                'team_id'=>$post['team_id']
            ];
            $res = Db::name('gtour_gschedule')->insertGetId($date);
            if($res){
                return ['code'=>0];
            }

        }
    }

    /*
     * 删除班期
     */
    public function delPaiban()
    {
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $res = Db::name('gtour_gschedule')->where('tg_id',$post['tg_id'])->where('tg_date',$post['tg_date'])
                ->where('team_id',$post['teams_id'])->delete();
            if($res){
                return ['code'=>0];
            }
        }
    }

    public function uploadsImg()
    {
        $upload =  parent::uploadOneFileToQiniu('lv/team/goods/');

        if ($upload['code'] == 0){

            $addData = [
                'gt_id'     => Session::get('img_id'),
                'img_path'    => $upload['data'],
                'create_time' => date("Y-m-d H:i:s"),
                
            ];

            $res = Db::name('gtour_tour_img')->insertGetId($addData);

            if ($res !== false){
                return ['code'=>0,'msg'=>'上传成功'];
            }else{
                return ['code'=>1,'msg'=>'上传失败'];
            }

        }else{

            return $upload;

        }

    }

}